package work.SlidingWindow;

import java.util.HashSet;
import java.util.Set;

public class Problem3 {

    public int lengthOfLongestSubstring(String s) {
        //滑动窗口
        int ans = 0;
        char[] charArray = s.toCharArray();
        Set<Character> set = new HashSet<>();
        for (int i = 0, j = 0; j < s.length(); j++) {

            while (set.contains(charArray[j])) {
                set.remove(charArray[i]);
                i++;
            }
            set.add(charArray[j]);
            ans = Math.max(set.size(), ans);
        }
        return ans;
    }
    public static void main(String[] args) {
        Problem3 problem = new Problem3();
        String testString = "pwwkew";
        int result = problem.lengthOfLongestSubstring(testString);
        System.out.println("The length of the longest substring without repeating characters in \"" + testString + "\" is: " + result);
    }
}
